package com.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import redis.clients.jedis.Pipeline;
import hyl.base.cache.redis.CRedis;
import hyl.base.cache.redis.RedisPool;
import hyl.ext.base.MyToken;
import hyl.ext.base.TokenFactory;
import hyl.ext.web.ms.TMsB;
import hyl.ext.ws.WsServe;
import hyl.core.Amy;
import hyl.core.MyFun;
import hyl.core.conf.MyConfig;
import hyl.core.conf.MyConst;
import hyl.core.fun.MyByte;
import hyl.core.info.CMsg;
import hyl.core.net.MyMail;
import hyl.core.run.IFinishT;
import hyl.core.run.MyTimer;

/**
 * 函数和参数放到M里面写 My里面只写与内核参数调整相关的代码
 * 
 * @author 阿友 37798955@qq.com
 *
 */
public class My extends Amy {
	protected static My my;

	static {
		DB1 = "db1";
	}
	// protected static MyKafka _kfkp;
	TMsB _mB;
	static MyMail _mail;
	public static final String s系统名称 =MyConst.getInstance().get("系统中文名","好运来管理系统");
	public static byte[] bsm4key;

	private My() {
		bsm4key = MyByte.hexToBytes(MyFun.getN位随机HEX数(32));

		TokenFactory.getInstance();

		// _mB=TMsB.getInstance();
		// _mB.ssologin=new SqlOfUser();

	}

	IFinishT<CMsg> on收到消息处理 = new IFinishT<CMsg>() {
		// 返回false 表示继续处理消息 ,true 表示终止继续处理
		@Override
		public void success(CMsg o) {
			// 收到消息 以后 通过
			try {
				MyFun.print("on收到消息处理", o.toJsonString());
				String oString = o.g内容_文本();
				MyFun.print("getS内容", oString);
				String[] params = oString.split(":");
				WsServe.cmd(o.s标签, params[0], params[1]);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		@Override
		public void error(CMsg o, Throwable e) {

		}

	};

	/*
	 * public static MyKafka getKfkP() { return _kfkp; }
	 */

	public static My getInstance() {
		if (my == null) {
			my = new My();
		}
		// _kfkp = MyKafka.getInsProducer();

		TaskMission();
		return my;
	}

	public TMsB getMsb() {
		return _mB;
	}

	public static void reset() {
		my = new My();

		MyTimer.close("日结算");
		TaskMission();
	}
	/**
	 * 管理员使用
	 * 
	 * @param uuid
	 * @return
	 */
//	public static MyToken getToken(String uuid) {
//		TokenFactory.getInstance();
//		return TokenFactory.getTokenById(uuid);
//	}

	/**
	 * 适用于直接访问的jsp页面
	 */
	public static int getMyTokenUserID(HttpServletRequest req) {
		MyToken ses = getToken(req);
		return ses.getUserId();
	}

	/**
	 * 适用于直接访问的jsp页面 2019.3.5 sx
	 */
	public static int getMyTokenRoleId(HttpServletRequest req) {
		MyToken ses = getToken(req);
		return ses.getRoleid();
	}

	public static MyToken getToken(HttpServletRequest req) {
		MyToken tk = TokenFactory.getToken(req);
		if (tk == null) { // 如果没有就新建一个
			tk = TokenFactory.newToken(req);
		}
		return tk;
	}

	public static int getLanguage(HttpServletRequest req, HttpServletResponse res) {
		TokenFactory.getInstance();
		return TokenFactory.getToken(req).getLang();
	}

	public synchronized static Pipeline getRd1(int idx) {
		// 通过连接创建DB对象 操作sql
		return RedisPool.getInstance().getRedis("redis1", idx).createPipeline();
	}

	public synchronized static CRedis createRd1(int idx) {
		// 通过连接创建DB对象 操作sql
		return RedisPool.getInstance().getRedis("redis1", idx);
	}

	public static void TaskMission() {
		/*
		 * MyTimer.getInstance("日结算").dayStart( new IDo<Calendar>() {
		 * 
		 * @Override public void run(Calendar obj) throws Exception { Calendar ca =
		 * Calendar.getInstance(); ca.setTime(new Date()); ca.add(Calendar.DATE, -1);
		 * Date lastMonth = ca.getTime(); SimpleDateFormat sdf = new
		 * SimpleDateFormat("yyyyMMdd"); int a =
		 * Integer.parseInt(sdf.format(lastMonth)); SqlOfReward.cnt_日结算(a); } },
		 * MyFun.str2Datetime("2019-4-23 00:01:00"));
		 */
	}

	public static void exit() {
		closeAllThreads();
		closeDb();
		closeRd();
		closeMail();
	}

	public static String get系统名称() {
		return s系统名称;
	}

	public static void sendMail(String 收件人, String 主题, String 内容) {
		if (_mail == null)
			_mail = MyMail.getInstance("hyl");
		_mail.send(收件人, 主题, 内容);
	}

	public static void sendSMS(String 手机, String 内容) {

	}

	private static void closeMail() {
		_mail.close();
		Log.info("smtp 连接正常关闭...");
	}

	private static void closeRd() {
		RedisPool.getInstance().close();
		Log.info("redis 连接池正常关闭...");
	}

	public static void onErrClose() {
		onExit = new Runnable() {
			@Override
			public void run() {
				Log.info("异常关闭触发事件");
				exit();
			}
		};
	}

	public static void main(String[] args) {
		MyFun.print(MyConfig.getConfOf屏蔽());
		MyFun.print(MyConfig.getConfOf消息());
		MyFun.print(MyConfig.getConfOf路由());
	}
}
